package com.edu.learning.concurrent.custom;

import com.edu.learning.concurrent.custom.queue.IBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 消费者
 *
 * @Author ll
 * @Date 2017/10/16 17:22
 */
public class Consumer implements Runnable {

    private IBlockingQueue<String> queue;

    public Consumer(IBlockingQueue<String> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        while (true) {
            String data = null;
            try {
                data = queue.take();
                System.out.println("Consumer " + Thread.currentThread().getName() + " consumer: " + data);
                if ("FIN".equals(data)) {
                    break;
                }
                TimeUnit.MICROSECONDS.sleep(100);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
        System.out.println(Thread.currentThread().getName() + " : Consumer over");
    }
}
